package br.com.mtg.movethegarbage.screens;

import br.com.mtg.movethegarbage.framework.Game;
import br.com.mtg.movethegarbage.framework.GameTouchEvent;
import br.com.mtg.movethegarbage.framework.Input;
import br.com.mtg.movethegarbage.framework.Screen;
import br.com.mtg.movethegarbage.game.Assets;
import br.com.mtg.movethegarbage.game.Score;
import br.com.mtg.movethegarbage.game.ScoreList;
import br.com.mtg.movethegarbage.graphics.Camera2D;
import br.com.mtg.movethegarbage.graphics.SpriteManager;
import br.com.mtg.movethegarbage.impl.ArrowButton;
import br.com.mtg.movethegarbage.impl.Button;

public class ScoreScreen extends Screen
{
	ScoreList scoreList;
	Button prevLvl, nextLvl;
	Button up, down;
	Button buttonBack;
	int cursor, lvlCursor;
	Camera2D cam;
	Score[] scoreListArray;
	SpriteManager spriteManager;

	public ScoreScreen(Game _game)
	{
		super(_game);

		width = game.getGraphics().getWidth();
		height = game.getGraphics().getHeight();
		multiplier = game.getGraphics().getMultiplier();
		cam = new Camera2D(game.getGraphics());
		spriteManager = new SpriteManager(game.getGraphics(), 1);

		nextLvl = new Button(width - (40 * multiplier + 10), height / 2 - 40 * multiplier / 2, 40 * multiplier, 40 * multiplier);
		prevLvl = new Button(10, height / 2 - 40 * multiplier / 2, 40 * multiplier, 40 * multiplier);

		up = new ArrowButton(width / 4 - 40 * multiplier / 2, height - 40 * multiplier - 10, 40 * multiplier, 40 * multiplier, ArrowButton.DIRECTION_UP);
		down = new ArrowButton(width / 4 - 40 * multiplier / 2, 10, 40 * multiplier, 40 * multiplier, ArrowButton.DIRECTION_DOWN);

		buttonBack = new Button(10, 10, 40 * multiplier, 40 * multiplier);
		buttonBack.setButtonSprite(Assets.buttonBack);

		up.setButtonSprite(Assets.buttonArrowGreen);
		down.setButtonSprite(Assets.buttonArrowGreen);

		prevLvl.setButtonSprite(Assets.buttonArrowWhite1);
		nextLvl.setButtonSprite(Assets.buttonArrowWhite2);

		cursor = 0;
		lvlCursor = 1;
		scoreList = new ScoreList(game);
	}

	@Override
	public void update(long delta)
	{
		Input input = game.getInput();

		synchronized (input)
		{
			for (GameTouchEvent e : input.getTouchEvents())
			{
				up.handleEvent(e, game);
				down.handleEvent(e, game);

				nextLvl.handleEvent(e, game);
				prevLvl.handleEvent(e, game);

				buttonBack.handleEvent(e, game);
			}
			input.getTouchEvents().clear();
		}
		if (buttonBack.wasPressed() || input.wasBackPressed())
			game.setScreen(new MainMenuScreen(game));
		if (up.wasPressed())
		{
			if (scoreListArray != null)
				cursor = (cursor == 0 ? 0 : (cursor - 5 >= 0 ? cursor - 5 : cursor));
		}
		else if (down.wasPressed())
		{
			if (scoreListArray != null)
				cursor = (cursor + 5 < scoreListArray.length ? cursor + 5 : cursor);
		}
		else if (nextLvl.wasPressed())
		{
			lvlCursor = (lvlCursor == 20 ? 1 : lvlCursor + 1);
			cursor = 0;
		}
		else if (prevLvl.wasPressed())
		{
			lvlCursor = (lvlCursor == 1 ? 20 : lvlCursor - 1);
			cursor = 0;
		}
	}

	@Override
	public void present(long delta)
	{
		spriteManager.beginDraw(Assets.tex_backgroundScreenRedMain);
		spriteManager.drawSprite(0, 0, width, height, Assets.background);
		spriteManager.endDraw();

		up.draw(game.getGraphics());
		down.draw(game.getGraphics());
		prevLvl.draw(game.getGraphics());
		nextLvl.draw(game.getGraphics());
		buttonBack.draw(game.getGraphics());

		scoreListArray = scoreList.getScoreArray("Level" + lvlCursor);

		Assets.font.setScale(0.8F * multiplier);

		Assets.font.printAt("Level " + lvlCursor, width - Assets.font.getTextLength("Level " + lvlCursor), height - Assets.font.getTextHeight());
		if (scoreListArray != null)
		{
			int max = (cursor + 5 <= scoreListArray.length ? cursor + 5 : scoreListArray.length);
			for (int i = cursor; i < max; i++)
			{
				Assets.font.printAt(scoreListArray[i].toString(), 50 * multiplier, height - (i - cursor) * Assets.font.getTextHeight() - 70 * multiplier);
			}
		}
	}

	@Override
	public void pause()
	{
	}

	@Override
	public void resume()
	{
		cam.setViewAndMatrices();

		game.getGraphics().initialize();

		width = game.getGraphics().getWidth();
		height = game.getGraphics().getHeight();
		multiplier = game.getGraphics().getMultiplier();
	}

	@Override
	public void dispose()
	{

	}

}
